<?php
class diaocha extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的


		$this->tit = '调查';
		$this->modelObj = D('company_diaocha');
		$this->adminObj = D('company_admin');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');

		$this->area_list = $this->areaObj->get_TreeList("status=1 ".$this->cond_base);
		$this->cate_list = $this->cateObj->get_TreeList("status=1 ".$this->cond_base);
		$this->zone_list = $this->zoneObj->get_TreeList("status=1 ".$this->cond_base);

		$cond = "status=1 ".$this->cond_base;
		$this->admins = $this->adminObj->fetchAll(array($cond),'id asc');
	}

    //列表
	public function index(){
		$this->tips = 1;
		$this->tabtit = array('全部','隐藏');
		$cond = "1=1".$this->cond_base;
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		$result = $this->modelObj->get_pagelist($cond,'sort asc,id asc');

		if($result){
			$i = 1;
			foreach ($result['list'] as $val) {

				if($val['admin_ids']){
					$val['admins'] = explode(',',$val['admin_ids']);
				}
				if($val['shifu_ids']){
					$val['shifus'] = explode(',',$val['shifu_ids']);
				}
				// // 绑定的师傅
				// $cond = "status=1 and (level=3)" .$this->cond_base;
				// $cond .= " and find_in_set('{$val['id']}',diaocha_ids)";
				// // dump($cond);exit;
				// $admins = $this->adminObj->findAll(array($cond),'id asc','realname');
				// if($admins){
				// 	$val['admins'] = $admins;
				// }

				$val['i'] = $i;
				$i = $i + 1;
                $list[] = $val;
            }
			$this->list = $list;
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}

	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data['company_id'] = $this->company['id'];
			$res = $this->modelObj->add($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}

		$options = ['非常满意','较满意','一般满意','不满意','不了解'];
		$this->options = $options;
		$this->info = array('sort'=>100,'status'=>1);
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){

			$data = $this->postData('data');

			$options = $this->postData('options');
			$data['options'] = implode('|',$options);

			$data['company_id'] = $this->company['id'];
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);
		$this->options = explode('|', $this->info['options']);
		// dump($this->info);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //审核
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}

		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}

	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}

	//审核
	public function act_keshi()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status,'is_keshi');
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}


	//编辑
	public function auth(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			// $arrBase['area_ids'] = joinStr($data['area']);
			// $arrBase['cate_ids'] = joinStr($data['cate']);
			// $arrBase['zone_ids'] = joinStr($data['zone']);

			$arrBase['admin_ids'] = joinStr($data['admin']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		// $cond_area = "status=1 ".$this->cond_base;
		// $cond_cate = "status=1 ".$this->cond_base;
		// $cond_zone = "status=1 ".$this->cond_base;


		// $this->areaList = $this->areaObj->get_JsTree($cond_area,$this->info['area_ids']);
		// $this->cateList = $this->cateObj->get_JsTree($cond_cate,$this->info['cate_ids']);
		// $this->zoneList = $this->zoneObj->get_JsTree($cond_zone,$this->info['zone_ids']);

		$cond_admin = "status=1 ".$this->cond_base;
		$this->adminList = $this->adminObj->get_JsTree($cond_admin,$this->info['admin_ids'],'realname');
	}


	//排序
	public function qrcode1()
	{
		require(APP_DIR.'/protected/include/lib/phpqrcode.php');

        $id = arg('id');
		$status = arg('status');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$url = $this->setting['site']['site_url'].'/index/diaocha/add?bc_id=='.$id;
				$info = $this->modelObj->findById($id);
				$res = $this->modelObj->qrcode($url,$info,$this->company,'bingchuang');
				$this->renderSuccess('生成二维码成功');
			}else{
				if($opid){

					foreach($opid as $val){
						$url = $this->setting['site']['site_url'].'/index/order/add?zone_id='.$val;
						$info = $this->modelObj->findById($val);
						$res = $this->modelObj->qrcode($url,$info,$this->company,'bingchuang');
					}
					$this->renderSuccess('生成二维码成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

	//排序
	public function qrcode2()
	{
		require(APP_DIR.'/protected/include/lib/phpqrcode.php');

        $id = arg('id');
		$status = arg('status');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){


				$url = $this->setting['site']['site_url'].'/index/login/reg?zone_id='.$id;
				$info = $this->modelObj->findById($id);
				$res = $this->modelObj->qrcode($url,$info,$this->company,'reg');
				$this->renderSuccess('生成二维码成功');
			}else{
				if($opid){

					foreach($opid as $val){
						$url = $this->setting['site']['site_url'].'/index/login/reg?zone_id='.$val;
						$info = $this->modelObj->findById($val);
						$res = $this->modelObj->qrcode($url,$info,$this->company,'reg');
					}
					$this->renderSuccess('生成二维码成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}


	//排序
	public function zipdown()
	{

        $filePath = "./upload/bingchuang/{$this->company['id']}/";
		$zipFileName = "bingchuang_{$this->company['id']}.zip";

		$zip = new \lib\ZipService();
		$zip = $zip->createZip($filePath, $zipFileName);

		$file = $zipFileName;
		if (file_exists($file)) {
			header('Content-Description: File Transfer');
			header('Content-Type: application/zip');
			header('Content-Disposition: attachment; filename="'.basename($file).'"');
			header('Expires: 0');
			header('Cache-Control: must-revalidate');
			header('Pragma: public');
			header('Content-Length: ' . filesize($file));
			readfile($file);
			@unlink($file);
			exit;
		}
	}

	//调查权限
	public function auth_admin(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['admin_ids'] = joinStr($data['admin']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		// 绑定的师傅
		$cond = "status=1 and (level=3 or level=4 or level=5 or level=6)" .$this->cond_base;
		$cond .= " and find_in_set('{$this->info['id']}',diaocha_ids)";
		$cond_admin = $cond.$this->cond_base;
		$this->adminList = $this->adminObj->get_JsTree($cond_admin,$this->info['admin_ids'],'realname');

		$this->layout = "admin/layout2.php";
	}

	//调查权限
	// public function auth_shifu(){
	// 	$id = arg('id');
	// 	if($this->isAjax()){
	// 		$data = $this->postData('data');

	// 		$arrBase['shifu_ids'] = joinStr($data['shifu']);

	// 		$this->modelObj->edit($id,$arrBase);
	// 		$this->renderSuccess('操作成功', "/{$this->routeUrl}/index");
	// 	}
	// 	$this->info = $this->modelObj->findById($id);

	// 	// 绑定的师傅
	// 	$cond = "status=1 and (level=5 or level=6)" .$this->cond_base;
	// 	$cond .= " and find_in_set('{$this->info['id']}',diaocha_ids)";
	// 	$cond_shifu = $cond.$this->cond_base;
	// 	$this->shifuList = $this->adminObj->get_JsTree($cond_shifu,$this->info['shifu_ids'],'realname');

	// 	$this->layout = "admin/layout2.php";
	// }
}